Skip to content

feat: add encode_json and loads helpers for better JSON null support#57

Open
adityak74 wants to merge 2 commits intotoon-format:mainfrom
adityak74:json-null-support
Open

feat: add encode_json and loads helpers for better JSON null support#57
adityak74 wants to merge 2 commits intotoon-format:mainfrom
adityak74:json-null-support

Conversation

@adityak74
Copy link
Copy Markdown

Linked Issue

Closes #49

Description

Support JSON null values in the toon-python encoding flow by adding helper functions to automatically convert JSON null to Python None during encoding. This improves the developer experience when working with LLM tool outputs, APIs, and logs where JSON null is the standard for missing values.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test coverage improvement

Changes Made

  • Added toon_format.loads(json_string) as a convenience wrapper around json.loads to ensure a "TOON-friendly" integration flow.
  • Added toon_format.encode_json(json_string) to directly encode a JSON string into TOON format, handling null to None conversion automatically.
  • Exported loads and encode_json in the top-level toon_format package.
  • Added comprehensive integration tests in tests/test_json_integration.py covering null values in objects and arrays.

SPEC Compliance

  • This PR implements/fixes spec compliance
  • Spec section(s) affected: Null Values
  • Spec version:

Testing

  • All existing tests pass
  • Added new tests for changes
  • Tests cover edge cases and spec compliance

Pre-submission Checklist

  • My code follows the project's coding standards
  • I have run code formatting/linting tools
  • I have added tests that prove my fix/feature works
  • New and existing tests pass locally
  • I have updated documentation if needed
  • I have reviewed the TOON specification for relevant sections

Breaking Changes

  • No breaking changes

Additional Context

This implementation follows the proposed solution in the issue to provide a standard utility for JSON integration, reducing the need for manual pre-processing of data containing null values.

@adityak74 adityak74 requested a review from a team as a code owner April 16, 2026 04:38
@smortezah smortezah requested a review from Copilot April 16, 2026 21:24
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds JSON integration helpers to toon_format to streamline encoding JSON strings (including JSON null) into TOON, and introduces integration tests to validate the flow.

Changes:

  • Added toon_format.loads() (wrapper around json.loads) and toon_format.encode_json() (JSON string → parsed Python → encode()).
  • Re-exported the new helpers at the top-level toon_format package.
  • Added new integration tests covering null handling in objects and arrays.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
src/toon_format/utils.py Introduces loads() and encode_json() helpers and exports them from the utils module.
src/toon_format/__init__.py Re-exports the new helpers in the public package API.
tests/test_json_integration.py Adds integration tests for JSON null → Python None → TOON null behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_json_integration.py Outdated
Comment thread tests/test_json_integration.py Outdated
Comment thread tests/test_json_integration.py Outdated
Comment thread src/toon_format/utils.py Outdated
Comment thread src/toon_format/utils.py
- Update utils.py module docstring to include JSON helpers
- Fix circular import of encode in utils.py
- Remove unused imports and print statements in tests
- Fix formatting in tests using ruff
@smortezah
Copy link
Copy Markdown
Contributor

@toon-format/python-maintainers, this PR introduces new functions, loads() and encode_json(), and exposes them to the public API. To maintain alignment with the original TypeScript implementation of toon-format, I suggest we avoid adding excessive functions. Any thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support JSON null values (auto-convert to Python None) in toon-python encoding flow

3 participants